package com.bootdo.staff.service.impl;

import com.bootdo.common.utils.AppR;
import com.bootdo.common.utils.MD5Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.bootdo.staff.dao.StaffMenuDao;
import com.bootdo.staff.dao.StaffMenuRoleDao;
import com.bootdo.staff.dao.StaffUserDao;
import com.bootdo.staff.domain.StaffMenuRoleDO;
import com.bootdo.staff.domain.StaffUserDO;
import com.bootdo.staff.service.StaffUserService;



@Service
public class StaffUserServiceImpl implements StaffUserService {
	@Autowired
	private StaffUserDao staffUserDao;
	@Autowired
	private StaffMenuRoleDao staffMenuRoleDao;
	@Autowired
	private StaffMenuDao staffMenuDao;
	
	
	@Override
	public List<StaffUserDO> list(Map<String, Object> map){
		return staffUserDao.list(map);
	}	
	
	@Override
	public int count(Map<String, Object> map){
		return staffUserDao.count(map);
	}
	
	
	@Transactional
	@Override
	public int save(StaffUserDO staffUser){
		
		int count = staffUserDao.save(staffUser);
		List<Long> smenuIds = staffUser.getSmenuIds();
		System.out.println(smenuIds);
		Long staffId = staffUser.getStaffId();	
		System.out.println(staffId);
		 List<StaffMenuRoleDO> rms = new ArrayList<StaffMenuRoleDO>();
	        for (Long smenuId : smenuIds) {
	        	StaffMenuRoleDO smrd = new StaffMenuRoleDO();
	        	smrd.setStaffId(staffId);
	        	smrd.setSmenuId(smenuId);
	            rms.add(smrd);
	        }
	        staffMenuRoleDao.removeByUserId(staffId);
	        if (rms.size() > 0) {
	        	staffMenuRoleDao.batchSave(rms);
	        }
	        return count;		
	}
	
	@Transactional
	@Override
	public int remove(Long id){
		int count = staffUserDao.remove(id);		
		staffMenuRoleDao.removeByUserId(id); 
		return count;
	}
	
	@Override
	public StaffUserDO get(Long id){
		return staffUserDao.get(id);
	}
	
	@Override
	public int update(StaffUserDO staffUser){
		int r = staffUserDao.update(staffUser);
        List<Long> smenuIds = staffUser.getSmenuIds();
        Long staffId = staffUser.getStaffId();
        staffMenuRoleDao.removeByUserId(staffId);
        List<StaffMenuRoleDO> rms = new ArrayList<>();
        for (Long smenuId : smenuIds) {
        	StaffMenuRoleDO smDo = new StaffMenuRoleDO();
        	smDo.setStaffId(staffId);
            smDo.setSmenuId(smenuId);
            rms.add(smDo);
        }
        if (rms.size() > 0) {
        	staffMenuRoleDao.batchSave(rms);
        }
        return r;
	}
	
	
	@Override
	public int batchRemove(Long[] ids){
		return staffUserDao.batchRemove(ids);
	}

	public AppR login(String staffName , String password){
		StaffUserDO staffUserDo = staffUserDao.staffByName(staffName);
		if(staffUserDo==null)
			return AppR.error(1001,"用户不存在");


		System.out.println(MD5Utils.encrypt(staffName, password));
		if(!staffUserDo.getStaffPassword().equals(MD5Utils.encrypt(staffName, password)))
			return AppR.error(1002,"用户密码不正确");

		staffUserDo.setLoginTime(new Date());
		staffUserDao.update(staffUserDo);
		return AppR.ok(staffUserDo);
	}

	@Override
	public List<StaffUserDO> selectAll() {
		
		return staffUserDao.selectAll();
	}

	@Override
	public StaffUserDO selectOne() {
		
		return staffUserDao.selectOne();
	}

	@Override
	public List<StaffUserDO> listByStatus(String status) {
		
		Map<String, Object> param = new HashMap<>();
	        param.put("status", status);
	        return staffUserDao.list(param);
		
	}

	@Override
	public List<StaffUserDO> listStatus() {
		
		return staffUserDao.listStatus();
	}

	@Override
	public List<StaffUserDO> listWorker(Long smenuId) {
		
		return  staffUserDao.listWorker(smenuId);
	}
	
}
